package com.laker.admin.module.excel.facade.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.laker.admin.framework.constants.CodeTable;
import com.laker.admin.framework.model.Response;
import com.laker.admin.module.excel.application.ExcelTemplateApplication;
import com.laker.admin.module.excel.domain.ExcelTemplate;
import com.laker.admin.module.excel.entity.ExcelTemplateCmd;
import com.laker.admin.module.excel.facade.ExcelTemplateFacade;
import com.laker.admin.module.sys.entity.SysUser;
import com.laker.admin.module.sys.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author nihongxin
 * @date 2021/12/14
 * @description
 */
@Service
public class ExcelTemplateFacadeImpl implements ExcelTemplateFacade {

    @Resource
    private ExcelTemplateApplication excelTemplateApplication;

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public Response save(ExcelTemplate excelTemplate) {
        try {
            boolean flag = excelTemplateApplication.checkExcelTemplateName(excelTemplate);
            if (flag) {
                return Response.ok(excelTemplateApplication.save(excelTemplate));
            } else {
                return Response.error(String.valueOf(CodeTable.PARAM_ERR), "模版名称重复，保存失败");
            }
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response deleteById(Long id) {
        try {
            excelTemplateApplication.deleteById(id);
            return Response.ok("删除成功");
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response list(ExcelTemplateCmd excelTemplateCmd) {
        try {
            Page<ExcelTemplate> excelTemplatePage = excelTemplateApplication.listExcelTemplatePage(excelTemplateCmd.getDeptId(), excelTemplateCmd.getKeyword(), excelTemplateCmd.getPageNo(), excelTemplateCmd.getPageSize());
            return Response.ok(excelTemplatePage);
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response getById(Long id) {
        try {
            return Response.ok(excelTemplateApplication.getById(id));
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response listByUser(ExcelTemplateCmd excelTemplateCmd) {
        try {
            SysUser sysUser = sysUserService.getById(StpUtil.getLoginIdAsLong());
            Page<ExcelTemplate> excelTemplatePage = excelTemplateApplication.listExcelTemplatePage(sysUser.getDeptId(), excelTemplateCmd.getKeyword(), excelTemplateCmd.getPageNo(), excelTemplateCmd.getPageSize());
            return Response.ok(excelTemplatePage);
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response saveWfProcessName(Long id, String wfProcessName) {
        try {
            ExcelTemplate excelTemplate = excelTemplateApplication.getById(id);
            excelTemplate.setWfProcessName(wfProcessName);
            excelTemplateApplication.save(excelTemplate);
            return Response.ok();
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }
}
